package sg.nus.iss.vmcs.gui.elements;

import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.TextField;
import java.awt.event.ActionListener;

import sg.nus.iss.vmcs.utils.MoneyFormatter;

/**
 * Drinks Selection Item used in the DrinksSelection Panel
 * @author Philip Chew
 */
public class DrinkSelectionItem extends StoreViewerItem {

	/** Generated Serial ID */
	private static final long serialVersionUID = -7738702208094282204L;
	
	/** Button for the Drink Selection Item */
	private Button btn;
	
	/** TextField for the Price Display  */
	private TextField txtPriceDisplay;
	
	/** TextField for the Stock Display */
	private TextField txtStockDisplay;
	
	/** The quantity of drinks */
	private int drinkQty = 1;
	
	/**
	 * @param ids
	 */
	public DrinkSelectionItem(ItemDisplayStrategy ids) {
		super(ids);
		
		//Setting up UI
		btn = new Button();
		btn.setBackground(Color.white);
		
		txtPriceDisplay = new TextField();
		txtPriceDisplay.setEditable(false);
		txtPriceDisplay.setForeground(Color.yellow);
		txtPriceDisplay.setBackground(Color.black);
		
		txtStockDisplay	= new TextField("NOT IN STOCK");
		txtStockDisplay.setEditable(false);
		
		ids.setupPanelLayout(this, btn, txtPriceDisplay, txtStockDisplay);
	}

	/* 
	 * Returns textfield to be set
	 */
	@Override
	public Component getDisplayComponent() {
		return txtStockDisplay;
	}

	/* 
	 * Sets the Variable Display State
	 */
	@Override
	public void setDisplay() {
		vds.setDisplayComponent(this);
	}

	/**
	 * Sets the name of the drink
	 * @param name Name of the drink
	 */
	@Override
	public void setName(String name) {
		btn.setLabel(name);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerItem#setQuantity(int)
	 */
	@Override
	public void setQuantity(int qty) {
		if(drinkQty == 0 && qty > 0) {
			this.setActive(true);
			setDisplay();
		}
		else if(drinkQty > 0 && qty < 1) {
			this.setActive(false);
			setDisplay();
		}
		
		drinkQty = qty;
	}

	/*
	 * Sets the price of the display
	 */
	@Override
	public void setValue(String value) {
		txtPriceDisplay.setText(MoneyFormatter.toMoney(Integer.parseInt(value)));
	}

	@Override
	public void addListener(ActionListener listener) {
		btn.addActionListener(listener);
		
	}

	@Override
	public void setActionCommand(String command) {
		btn.setActionCommand(command);
		
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerItem#setActive(boolean)
	 */
	@Override
	public void setActive(boolean active) { 
		if(drinkQty > 0) {
			this.btn.setEnabled(active);
			this.txtPriceDisplay.setEnabled(active);
		}
		else {
			this.btn.setEnabled(false);
			this.txtPriceDisplay.setEnabled(false);
		}
	}
	
	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.models.StoreItemObserver#notifyChange(java.lang.String, int, int)
	 */
	@Override
	public void notifyChange(String name, int qty, int value) {
		this.setName(name);
		this.setQuantity(qty);
		this.setValue(value);
	}
}
